/*
 * ChessMouseMotionListener.java
 *
 * Created on 27.11.2007, 18:16:53
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.chess.gui.components;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import cz.possoft.client.chess.gui.panel.ChessPanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.serializable.generic.Location;

/**
 *
 * @author mara
 *
 * listener nastava pri pohybu mysi nad sachovnici
 */
public class ChessMouseMotionListener extends MouseMotionAdapter {

    public ChessMouseMotionListener() {
    }
    
    public void mouseMoved(MouseEvent me) {
    	if (!(PanelHolderUtils.getCurrentGamePanel() instanceof ChessPanel)) {
    		return;
    	}    	
        if (PanelHolderUtils.getCurrentGamePanel().getGame().isActive()) {
            Location location = getMouseLocation(me);  
            ChessPanel chessPanel = (ChessPanel)PanelHolderUtils.getCurrentGamePanel();
            int state = chessPanel.getGameState();
            //System.out.println(state);
            if (state < 3) {
            	chessPanel.updateRingFigurkaAndRepaint(location);
            }
            if ((state == 3) || (state == 4)) {
            	chessPanel.updateRingForPlaceWhereFigurkaCanPlay(location);
            }            
        }        
    }

    // vraci umisteni kde se prave nachazi mys ([0..7, 0..7])
    public static Location getMouseLocation(MouseEvent me) {
        int xx = me.getX();
        int yy = me.getY();
        if ((xx < 5) || (xx > 443) || (yy < 5) || (yy > 443)) {
            return new Location(-1, -1);
        } else {        	
        	int xCil = (xx - 4) / 55;
        	int yCil = (yy - 4) / 55;
        	// pro cerneho hrace musime zmenit lokaci (otoceni sachovnice)
        	if (!PanelHolderUtils.getCurrentGamePanel().getGame().isMeWhite()) {
        		xCil = 7 - xCil;
        		yCil = 7 - yCil;
        	}        	
        	// navrat lokace
            return new Location(xCil, yCil);
        }
    }
}
